﻿using Azylee.Core.IOUtils.DirUtils;
using Azylee.Core.IOUtils.FileUtils;
using Azylee.Core.IOUtils.TxtUtils;
using Azylee.Core.Plus.DataUtils.JsonUtils;
using Azylee.Core.VersionUtils;
using Azylee.Update.UpdateUtils;
using Azylee.YeahWeb.HttpUtils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JustMetClient.Packager.Views.MainViews
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btPackage_Click(object sender, EventArgs e)
        {
            string src = tbPath.Text;

            //获取版本号
            Version version = VersionTool.GetVersion(DirTool.Combine(src, @"\Release\JustMetClient.exe"));
            //设置Ftp文件路径名称
            string pkgFile = string.Format("{0}[{1}].udp", tbName.Text, version);
            string vsFile = string.Format("{0}.version", version);
            tbFtpFile.Text = @"JustMetClient\Update\JustMetClient\" + pkgFile;

            if (version != null)
            {
                //修改文件名称为版本号
                Directory.Move(DirTool.Combine(src, @"\Release"), DirTool.Combine(src, @"\", version.ToString()));

                tbVersion.Text = version.ToString();

                string pkgDst = DirTool.Combine(DirTool.GetFilePath(tbPath.Text), pkgFile);
                string vsDst = DirTool.Combine(DirTool.GetFilePath(tbPath.Text), vsFile);

                if (FilePackageTool.Pack(src, pkgDst) > 0 && File.Exists(pkgDst))
                {
                    string md5Code = FileTool.GetMD5(pkgDst);
                    //设置更新模型，14个属性
                    AppUpdateInfo aui = new AppUpdateInfo()
                    {
                        Name = tbName.Text.Trim(),
                        Author = tbAuthor.Text.Trim(),
                        Desc = tbDesc.Text.Trim(),
                        Version = tbVersion.Text.Trim(),
                        ReleasePath = tbReleasePath.Text.Trim(),
                        Necessary = cbNecessary.Checked,
                        DateTime = DateTime.Now,
                        DownloadMode = rbHttpMode.Checked ? 0 : 1,
                        HttpUrl = tbHttpUrl.Text.Trim(),
                        FtpIp = tbFtpIp.Text.Trim(),
                        FtpAccount = tbFtpAccount.Text.Trim(),
                        FtpPassword = tbFtpPassword.Text.Trim(),
                        FtpFile = tbFtpFile.Text.Trim(),
                        Md5 = md5Code,
                    };
                    string auiJson = JsonTool.ToStr(aui);
                    TxtTool.Create(vsDst, auiJson);
                    
                    string rs = HttpTool.Get<string>(string.Format("{0}{1}", TBServerAPI.Text, tbVersion.Text));
                }
            }
        }
    }
}
